<?php
$this->pageTitle = 'Manage Categories';
$this->breadcrumbs = array(
	'Categories'=>array('index'),
	'Manage',
);



//$this->menu=array(
//	array('label'=>'Create Categories', 'url'=>array('create')),
//);
//
//Util::registerScript('search', "
//$('.search-button').click(function(){
//	$('.search-form').toggle();
//	return false;
//});
//$('.search-form form').submit(function(){
//	$.fn.yiiGridView.update('categories-grid', {
//		data: $(this).serialize()
//	});
//	return false;
//});
//
//");


?>
<div class="heading">
    <h1><img src="<?php echo Util::themeUrl() ?>/images/default.png" /> Manage Categories</h1>

    <div class="controls">
        <?php
        echo CHtml::button('Create', array('class' => 'controls', 'id' => 'create-categories'));
        echo CHtml::button('Delete', array('class' => 'controls', 'id' => 'delete-categories'));
        ?>
    </div>
</div>

<div class="content">

<?php
// echo CHtml::link('Advanced Search','#',array('class'=>'search-button'));
// echo '<div class="search-form" style="display:none">';
// $this->renderPartial('_search',array(
//	'model'=>$model,
// ));
// echo '</div><!-- search-form -->';
?>


<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'categories-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
        'cssFile' => Util::themeUrl() . '/css/gridview.css',
        'htmlOptions'=>array('class'=>'grid-view clear'),
        'afterAjaxUpdate'=>'js:function(id,data){$.bind_data()}',
	'columns'=>array(
		array(
            'header' => 'No.',
            'value' => '$row + 1',
            'htmlOptions' => array(
                'style' => 'width:25px; text-align:center'
            )
        ),
        array(
            'class' => 'CCheckBoxColumn',
            'selectableRows' => 2,
            'id' => 'chk',
            'value' => '$data->id',
        ),
        'image' => array(
					'name' => 'image',
					'type' => 'raw',
		        	'value'=>'CHtml::image(Util::getImageExist("category", $data->image), "category",  array("width" => "70", "height" => "70", "border" => "0"))',
					'htmlOptions' => array(
		                'style' => 'width:80px;text-align:center'
		            ),
		        	'filter' => false,
		),
		'name'=> array(
        	'name' => 'name',
			'htmlOptions' => array(
                'style' => 'width:250px;',
            )
		),
        'code' => array(
        	'name' => 'code',
			'htmlOptions' => array(
                'style' => 'width:90px; text-align:center',
            )
		),
		'description' => array(
                    'name' => 'description',
                    'type' => 'raw',
                ),
         'position' => array(
        	'name' => 'position',
			'htmlOptions' => array(
                'style' => 'width:50px;text-align:center',
            )
		),
		'status' => array(
                    'name' => 'status',
                    'type' => 'raw',
                    'value' => 'CHtml::link(CHtml::image(Util::themeUrl(). "/images/" . (($data->status==1)?"tick_circle.png":"cross_circle.png")), array("update_status", "id"=>$data->id), array("class" => "update-status"))',
                    'filter' => array(0=>'Deactive', 1=>'Active'),
                	'htmlOptions' => array(
		                'style' => 'width:90px; text-align:center',
		            )
                ),
		array(
						'class'					=>'CButtonColumn',
                        'viewButtonImageUrl' 	=> false,
                        'updateButtonImageUrl' 	=> false,
                        'deleteButtonImageUrl' 	=> false,
                        'template' 				=> '[{update}]',
                        'htmlOptions' 			=> array(
			                'style' => 'width:70px; text-align:center'
			            )

		),
	),
)); ?>
</div>

<script  type="text/javascript">
    $(document).ready(function() {
        $. bind_data = function(){
            $(".grid-view a.view").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('categories-grid')
                    }
                });
                return false;
            });

            $(".grid-view a.update").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('categories-grid')
                    }
                });
                return false;
            });
        }

        // bind data
        $. bind_data();

        $("#create-categories").click(function() {
                $.fancybox.open({
                    href : '<?php echo $this->createUrl('create', array('ajax' => true)) ?>',
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('categories-grid')
                    }
                });
                return false;
            });

        $('#delete-categories').live('click', function(){
            var count=0;
            var params = 'YII_CSRF_TOKEN=<?php echo  Yii::app()->request->csrfToken; ?>';
            $("input[name='chk[]']").each(function(){
            if($(this).is(":checked")){
                count++;
                params += '&chk[]=' + $(this).val();
            }
            });
            if(count >= 1 ){
            if(!confirm("Delete selected records"))
                return false;
            }else{
                alert("No checkbox is selected");
                return false;
            }

            $.ajax({
                    type: "POST",
                    url: '<?php echo $this->createUrl('delete', array('ajax' => true)) ?>',
                    data: params,
                    success: function(data) {
                        $.fn.yiiGridView.update('categories-grid');
                    } //success
                });//ajax
                return false;
        });
    });
</script>